package cn.longge.mapper;

import cn.longge.entity.SysOffice;
import cn.longge.entity.SysResource;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

public class SysRoleSqlProvider {

    public String selectRoleAndOffice(Map<String,Object> params){
        return new SQL(){
            {
                SELECT("sr.id,sr.name,sr.data_scope,sr.remarks,so.`name` as office_name");
                FROM("sys_role sr,sys_office so");
                WHERE("sr.del_flag = '0'");
                WHERE("so.del_flag = '0'");
                WHERE("sr.office_id = so.id");
                if(params.containsKey("dataScope") && !StringUtils.isEmpty(params.get("dataScope"))){
                    WHERE(" data_scope = #{dataScope}");
                }
                if(params.containsKey("officeName") && !StringUtils.isEmpty(params.get("officeName"))){
                    WHERE(" so.`name` = #{officeName}");
                }
                if(params.containsKey("name") && !StringUtils.isEmpty(params.get("name"))){
                    WHERE(" sr.`name` like concat('%',#{name},'%') ");
                }
                if(params.containsKey("remarks") && !StringUtils.isEmpty(params.get("remarks"))){
                    WHERE(" sr.remarks like concat('%',#{remarks},'%') ");
                }
            }
        }.toString();
    }

    public String deleteByUserIds(@Param("rid") Long rid,@Param("ids")List<Long> ids){
        StringBuilder sb = new StringBuilder();
        sb.append("delete from sys_user_role where role_id = #{rid} and user_id in (");
        for (int i = 0; i < ids.size(); i++) {
            sb.append("#{ids[" + i + "]},");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(")");
        System.out.println(sb.toString());
        return sb.toString();
    }

    public String insertMany(@Param("rid")Long rid,@Param("addIds") List<Long> addIds){
        return new SQL(){
            {
                INSERT_INTO("sys_user_role");
                INTO_COLUMNS("role_id,user_id,del_flag");
                for (int i = 0; i < addIds.size(); i++) {
                    INTO_VALUES("#{rid}");
                    INTO_VALUES("#{addIds[" + i + "]}");
                    INTO_VALUES("0");
                    ADD_ROW();
                }
            }
        }.toString();
    }

    public String insertRoleResource(@Param("roleId") long roleId, @Param("resources")List<SysResource> resources){
        return new SQL(){
            {
                INSERT_INTO("sys_role_resource");
                INTO_COLUMNS("role_id,resource_id,del_flag");
                for (int i = 0; i < resources.size(); i++) {
                    INTO_VALUES("#{roleId}");
                    INTO_VALUES("#{resources[" + i + "].id}");
                    INTO_VALUES("0");
                    ADD_ROW();
                }
            }
        }.toString();
    }


    public String insertRoleOffice(@Param("roleId") long roleId, @Param("offices")List<SysOffice> offices){
        return new SQL(){
            {
                INSERT_INTO("sys_role_office");
                INTO_COLUMNS("role_id,office_id,del_flag");
                for (int i = 0; i < offices.size(); i++) {
                    INTO_VALUES("#{roleId}");
                    INTO_VALUES("#{offices[" + i + "].id}");
                    INTO_VALUES("0");
                    ADD_ROW();
                }

            }
        }.toString();
    }
}
